import { Injectable } from '@nestjs/common'
import { CreateAcrossDto } from './dto/create-across.dto'
import { UpdateAcrossDto } from './dto/update-across.dto'
import { PrismaService } from 'src/prisma/prisma.service'
import { BetService } from 'src/bet/bet.service'

@Injectable()
export class AcrossService {
  constructor(
    private prisma: PrismaService,
    private bet: BetService,
  ) {}

  /**
   * 单个添加数据
   * @param createAcrossDto 添加所需要的参数
   * @returns 添加的数据
   */
  async create(createAcrossDto: CreateAcrossDto) {
    const { btName, sectionId } = createAcrossDto

    // 查询所有的bets
    const bets = await this.prisma.bet.findMany({ where: { sectionId } })
    const betId = bets.find((b) => b.btName === btName).id

    return this.prisma.across.create({
      data: {
        ...createAcrossDto,
        betId,
      },
    })
  }

  /**
   * 查询所有数据
   * @param args 包含sectionId的参数
   * @returns 返回查询的数据
   */
  async findAll(args: Record<string, any>) {
    const { sectionId, search, searchAcrossName } = args

    return await this.prisma.across.findMany({
      where: {
        sectionId: +sectionId,
        btName: { contains: search },
        acrossName: { contains: searchAcrossName },
      },
      include: {
        bet: true,
      },
    })
  }

  findOne(id: number) {
    return `This action returns a #${id} across`
  }

  async update(id: number, updateAcrossDto: UpdateAcrossDto) {
    return await this.prisma.across.update({
      where: { id },
      data: updateAcrossDto,
    })
  }

  remove(id: number) {
    return `This action removes a #${id} across`
  }

  /**
   * 删除sectionId的所有数据
   * @param id 区段编号 sectionId
   * @returns 删除的sectionId的所有数据的count
   */
  async removeAll(id: number) {
    return await this.prisma.across.deleteMany({ where: { sectionId: id } })
  }
}
